# python
from .. import db

from flask_restful import Resource, fields, marshal_with



user_fields = {
    'id': fields.Integer,
    'username': fields.String,  # 用户名
    'password': fields.String,  # 密码
    'nick_name':fields.String,  # 昵称,
    'role': fields.Integer,  # 角色 user admin 0 user 1 admin
    'avatar': fields.String,  # 头像
    'age': fields.Integer,  # 年龄
    'gender': fields.String,  # 性别
    'weight': fields.Float,  # 体重cm
    'height': fields.Float,  # 身高cm
    'illnesses': fields.String,  # 疾病
    'create_time': fields.DateTime,  # 创建时间
}

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), nullable=False)
    password = db.Column(db.String(120), nullable=False)
    nick_name = db.Column(db.String(80))
    avatar = db.Column(db.String(120))
    role = db.Column(db.Integer, nullable=False)
    age = db.Column(db.Integer)
    gender = db.Column(db.String(10))
    weight = db.Column(db.Float)
    height = db.Column(db.Float)
    illnesses = db.Column(db.String(120))
    create_time = db.Column(db.DateTime, server_default=db.func.now())

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'password': self.password,
            'nick_name': self.nick_name,
            'avatar': self.avatar,
            'role': self.role,
            'age': self.age,
            'gender': self.gender,
            'weight': self.weight,
            'height': self.height,
            'illnesses': self.illnesses,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')

        }

    def __repr__(self):
        return f'<User {self.username}>'
